<template>
  <div class="hello">
    <div class="global">
      <n-form-item class="px-2 pt-2" label="负荷1数据区域" path="form.capacityChargeTotalStation">
        <n-input name="Ccts" v-model:value="load1Range"
                 placeholder="选择区域后点击设置按钮"
                 :show-button="false"></n-input>
        <n-button type="primary" class="n-button mx-2 px-4" @click="setLoadRange(1)">设置</n-button>

        <n-input class="w-24" name="load1" v-model:value="load1" placeholder="负荷率"></n-input>
      </n-form-item>
      <n-form-item class="px-2" label="负荷2数据区域" path="form.capacityChargeTotalStation">
        <n-input name="Ccts" v-model:value="load2Range"
                 placeholder="选择区域后点击设置按钮"
                 :show-button="false"></n-input>
        <n-button type="primary" class="n-button mx-2 px-4" @click="setLoadRange(2)">设置</n-button>
        <n-input class="w-24" name="load1" v-model:value="load2" placeholder="负荷率"></n-input>

      </n-form-item>
      <n-form-item class="px-2" label="负荷3数据区域" path="form.capacityChargeTotalStation">
        <n-input name="Ccts" v-model:value="load3Range"
                 placeholder="选择区域后点击设置按钮"
                 :show-button="false"></n-input>
        <n-button type="primary" class="n-button mx-2 px-4" @click="setLoadRange(3)">设置</n-button>
        <n-input class="w-24" name="load1" v-model:value="load3" placeholder="负荷率"></n-input>
      </n-form-item>
      <n-form-item class="px-2" label="负荷3待插值数据区域" path="form.capacityChargeTotalStation">
        <n-input name="Ccts" v-model:value="load4Range"
                 placeholder="选择区域后点击设置按钮"
                 :show-button="false"></n-input>
        <n-button type="primary" class="n-button mx-2 px-4" @click="setLoadRange(4)">设置</n-button>
      </n-form-item>
      <n-button type="primary" class="w-full center" @click="cal()">计算插值</n-button>

    </div>
  </div>
</template>

<script setup lang="ts">
import axios from 'axios'
import { NForm, NFormItem, NInput, NButton } from 'naive-ui'
import EtRange = Et.EtRange
import { onMounted, ref } from 'vue'
import { pandas as pd, utils } from 'yklib'
import EtWorksheet = Et.EtWorksheet
import EtApplication = Et.EtApplication
import Series from './ts/pandas'


const load1Range = ref('')
const load2Range = ref('')
const load3Range = ref('')
const load4Range = ref('')
const selectedRangeId = ref(1)
const load1 = ref(1)
const load2 = ref(0.75)
const load3 = ref(0.50)

const setLoadRange = (id: number) => {
  selectedRangeId.value = id
}

/**
 * 根据已有复合点下的修正曲线数据，计算待定负荷点下的修正系数数据
 */
const cal = () => {
  let data1 = pd.DataFrame.readFromEtRange(window.Application, load1Range.value)
  let data2 = pd.DataFrame.readFromEtRange(window.Application, load2Range.value)
  let data3 = pd.DataFrame.readFromEtRange(window.Application, load3Range.value)
  let data4 = pd.DataFrame.readFromEtRange(window.Application, load4Range.value)
  let dataName1: Series = data1.get_series_by_index(0, 1)
  let dataName2: Series = data2.get_series_by_index(0, 1)
  data1.set_index(0)
  data1.print()
  let k1 = 0
  let k2 = 0
  let k3 = 0
  let b1 = 0
  let b2 = 0
  let b3 = 0
  let curves1: Array<[any, any]>
  let curves2: Array<[any, any]>
  if (dataName1.get(0) === 'C/H比') {
    // 判断辅助变量的数量，如相对湿度的修正曲线中，环境温度的数量
    let count = 0
    for (let i = 0; i < dataName1.size; i++) {
      if (dataName1.get(i) === 'C/H比') {
        count++
      }
    }
    curves1 = Array<[any, any]>(count)
    curves2 = Array<[any, any]>(count)

    // 将区域1中的数据拟合成曲线
    let current_curve_index = 0
    for (let i = 0; i < dataName1.size; i++) {
      let x: Series
      let y_p: Series
      let y_hr: Series
      if (dataName1.get(i) === 'C/H比') {
        current_curve_index++
        x = data1.get_row_by_index(i)
        y_p = undefined
        y_hr = undefined
      } else if (dataName1.get(i) === '功率修正系数') {
        y_p = data1.get_row_by_index(i)
      } else if (dataName1.get(i) === '热耗率修正系数') {
        y_hr = data1.get_row_by_index(i)
      }
      if (x && y_p && y_hr) {
        curves1.push([utils.interpolateRegression(x.values, y_p.values), utils.interpolateRegression(x.values, y_hr.values)])
      }
    }

    // 将区域2中的数据拟合成曲线
    current_curve_index = 0
    for (let i = 0; i < dataName2.size; i++) {
      let x: Series
      let y_p: Series
      let y_hr: Series
      if (dataName2.get(i) === 'C/H比') {
        current_curve_index++
        x = data2.get_row_by_index(i)
        y_p = undefined
        y_hr = undefined
      } else if (dataName2.get(i) === '功率修正系数') {
        y_p = data2.get_row_by_index(i)
      } else if (dataName2.get(i) === '热耗率修正系数') {
        y_hr = data2.get_row_by_index(i)
      }
      if (x && y_p && y_hr) {
        curves2.push([utils.interpolateRegression(x.values, y_p.values), utils.interpolateRegression(x.values, y_hr.values)])
      }
    }


  }
}


onMounted(() => {
  window.Application.ApiEvent.AddApiEventListener('SheetSelectionChange', () => {
    let app = window.Application
    let sheet = app.ActiveSheet
    let cells: EtRange = app.Selection as EtRange
    let address = `${sheet.Name}!${cells.Address(false, false)}`
    switch (selectedRangeId.value) {
      case 1:
        load1Range.value = address
        break
      case 2:
        load2Range.value = address
        break
      case 3:
        load3Range.value = address
        break
      case 4:
        load4Range.value = address
        break
      default:
        break
    }
  })
})

</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped lang="postcss">
@tailwind base;
@tailwind components;
@tailwind utilities;
.global {
  font-size: 15px;
  min-height: 95%;
}
</style>
